package com.elf.zero.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

/**
 * Activity结果
 */
public class ActivityResult {

    private final static int REQUEST_CODE = 9999;

    public static Builder with(Context context) {
        return new Builder(context);
    }

    public static class Builder {

        private final Context context;
        private ActivityResultListener listener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder listener(ActivityResultListener listener) {
            this.listener = listener;
            return this;
        }

        public void startActivity(Intent intent) {
            if (this.context instanceof FragmentActivity) {
                FragmentActivity activity = (FragmentActivity) this.context;
                FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
                ResultFragmentV4 fragment = new ResultFragmentV4();
                fragment.setIntent(intent, this.listener);
                transaction.add(android.R.id.content, fragment).commitAllowingStateLoss();
            } else if (this.context instanceof Activity) {
                Activity activity = (Activity) this.context;
                android.app.FragmentTransaction transaction = activity.getFragmentManager().beginTransaction();
                ResultFragment fragment = new ResultFragment();
                fragment.setIntent(intent, this.listener);
                transaction.add(android.R.id.content, fragment).commitAllowingStateLoss();
            } else {
                throw new RuntimeException("context must be activity");
            }
        }
    }

    /**
     * 结果监听
     */
    public static abstract class ActivityResultListener {
        /**
         * 成功回调
         *
         * @param data data
         */
        public void resultOK(Intent data) {
        }

        /**
         * 取消回调
         */
        public void resultCancel() {
        }

        /**
         * 完成回调，在成功/取消回到之后调用
         *
         * @param resultCode resultCode
         * @param data       data
         */
        public void complete(int resultCode, Intent data) {
        }

        public void exception(Exception e) {
        }
    }

    /**
     * v4.app.Fragment
     */
    public static class ResultFragmentV4 extends Fragment {

        private Intent intent;
        private ActivityResultListener listener;

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (listener != null) {
                if (Activity.RESULT_OK == resultCode) {
                    listener.resultOK(data);
                } else {
                    listener.resultCancel();
                }
                listener.complete(resultCode, data);
            }
            remove();
            super.onActivityResult(requestCode, resultCode, data);
        }

        public void setIntent(Intent intent, ActivityResultListener listener) {
            this.intent = intent;
            this.listener = listener;
        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            if (intent != null) {
                try {
                    startActivityForResult(intent, REQUEST_CODE);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (listener != null) {
                        listener.exception(e);
                    }
                    remove();
                }
            }
        }

        private void remove(){
            if (this.getActivity() != null) {
                this.getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
            }
            listener = null;
            intent = null;
        }
    }

    /**
     * app.Fragment
     */
    public static class ResultFragment extends android.app.Fragment {

        private Intent intent;
        private ActivityResultListener listener;

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (listener != null) {
                if (Activity.RESULT_OK == resultCode) {
                    listener.resultOK(data);
                } else {
                    listener.resultCancel();
                }
                listener.complete(resultCode, data);
            }
            remove();
            super.onActivityResult(requestCode, resultCode, data);
        }

        public void setIntent(Intent intent, ActivityResultListener listener) {
            this.intent = intent;
            this.listener = listener;
        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            if (intent != null) {
                try {
                    startActivityForResult(intent, REQUEST_CODE);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (listener != null) {
                        listener.exception(e);
                    }
                    remove();
                }
            }
        }

        private void remove(){
            if (this.getActivity() != null) {
                this.getActivity().getFragmentManager().beginTransaction().remove(this).commit();
            }
            listener = null;
            intent = null;
        }
    }
}
